<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Helper
 *前端助手类
 * @author martin
 */
class Helper {
    /**
     *字符串截取
     * @param type $string
     * @param type $length
     * @param type $etc
     * @return type string
     */
    public static function truncate_utf8($string, $length, $etc = '...')
    {
        $result = '';
        $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
        $strlen = strlen($string);
        for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
            {
            if($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
            {
                if ($length < 1.0)
                {
                    break;
                }
                $result .= substr($string, $i, $number);
                $length -= 1.0;
                $i += $number - 1;
            }
            else
            {
                $result .= substr($string, $i, 1);
                $length -= 0.5;
            }
        }
        $result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');
        if ($i < $strlen)
        {
            $result .= $etc;
        }
        return CHtml::encode($result);
    }
    
    /**
     *组装搜索语句
     * @param type $params $GET params array
     * @return type $criteria condition array
     */
    public static function search($params=array())
    {
        if(is_array($params))
        {
            if(array_key_exists('action',$params[0]))
                unset ($params[0]['action']);
                unset ($params[0]['id']);
                $resault = array();
                foreach($params[0] as $key=>$n)
                {
                    if(0 < strlen($n))
                        $resault[] = $key.'='.$n;
                }
                if(1 < count($resault))
                    $resault = implode(' and ',$resault);
                elseif(0 < count($resault))
                    $resault = $resault[0];
            else
                $resault = '';
                
            return array('condition'=>trim((string)$resault));
        }
    }
    
    /**
     *组装附加表语句
     * @param type $params $_GET params
     * @return type $criteria
     */
    public static function condition($params=array())
    {
        
        $resault = array();
        foreach($params as $key=>$n)
        {
            if(0 < strlen($n))
                $resault[] = $key.'="'.$n.'"';
        }
            $resault = implode(' and ',$resault);
        
         return array('condition'=>trim((string)$resault));
             //return array('condition'=>'name = "'.$params['name'].'" and money = "'.$params['money'].'" and color="'.$params['color'].'"');
    }
    /**
     *组装排序语句
     * @param type $params string
     */
    public function sort($params)
    {
        if(isset($params))
            return 't.'.str_replace('.desc',' desc',$params);
        else
            return null;
    }
}

?>
